package com.scs.application.core.utils;


import com.scs.application.core.consts.Const;
import com.scs.application.core.exception.BusinessException;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 */
@Slf4j
public class BeanUtils extends org.springframework.beans.BeanUtils {
    public BeanUtils() {
    }

    public static <S, T> void copyProperties(List<S> sourceList, List<T> targetList, Class<T> targetClass, boolean useBeanCopier) {
        T target = null;

        for(Iterator var5 = sourceList.iterator(); var5.hasNext(); targetList.add(target)) {
            Object source = var5.next();

            try {
                target = targetClass.newInstance();
            } catch (Exception var8) {
                throw new BusinessException("服务器错误");
            }

            if (useBeanCopier) {
                BeanCopierUtils.copy(source, target);
            } else {
                copyProperties(source, target);
            }
        }

    }

    public static <S, T> void copyProperties(List<S> sourceList, List<T> targetList, Class<T> targetClass) {
        copyProperties(sourceList, targetList, targetClass, false);
    }

    public static <S, T> List<T> copyProperties(List<S> sourceList, Class<T> targetClass, boolean useBeanCopier) {
        List<T> targetList = Lists.newArrayListWithExpectedSize(sourceList.size());
        copyProperties(sourceList, targetList, targetClass, useBeanCopier);
        return targetList;
    }

    public static <S, T> List<T> copyProperties(List<S> sourceList, Class<T> targetClass) {
        List<T> targetList = Lists.newArrayListWithExpectedSize(sourceList.size());
        copyProperties(sourceList, targetList, targetClass);
        return targetList;
    }


    /**
     * @Description：  复制对象属性
     * <pre>
     *     忽略父对象属性：即不复制DataEntity、FlowEntity等基类的属性。
     * </pre>
     * @param source            被复制的对象
     * @param target            需要复制到的对象  不能为空
     * @param isIgnoreParent   是否忽略父对象属性，true:忽略复制父对象属性
     *
     */
    public static void copyEntity(Object source, Object target,boolean isIgnoreParent){
        if (source == null) {
            return;
        }
        if (target == null) {
            BusinessException.throwError("copyEntity 参数错误，target不能为空");
        }
        copyProperties(source, target,isIgnoreParent ? Const.FIELDS_FLOW_ENTITY_AND_DATA_ENTITY : null);
    }

    /**
     * @Description：  批量复制对象属性
     * <pre>
     *     忽略父对象属性：即不复制DataEntity、FlowEntity等基类的属性
     * </pre>
     * @param sourceList            被复制的对象列表
     * @param targetList            需要复制到的对象列表  不能为空
     * @param targetClass           被复制的对象类，不能为空 初始化使用
     * @param isIgnoreParent        是否忽略父对象属性，true:忽略复制父对象属性
     *
     */
    public static  <S, T> void copyEntitys(List<?> sourceList, List<T> targetList, Class<T> targetClass, boolean isIgnoreParent) {
        if (targetList == null) {
            BusinessException.throwError("copyEntitys 参数错误，targetList不能为空");
        }
        if (sourceList == null || sourceList.size() == 0) {
            return;
        }
        T target = null;
        for(Object source : sourceList) {
            try {
                target = targetClass.newInstance();
            } catch (Exception e) {
                log.error("copyEntitys.error",e);
                targetList.clear();
                BusinessException.throwError("copyEntitys 操作失败，原因：" + (e !=null ? e.getMessage() : "未知异常"));
            }
            copyEntity(source,target,isIgnoreParent);
            targetList.add(target);
        }
    }
}
